package cn.lyd.base.controller;


import cn.lyd.base.dto.BinderDto;
import cn.lyd.base.dto.WechatDto;
import cn.lyd.base.service.ILoginService;
import cn.lyd.base.until.AjaxResult;
import cn.lyd.base.until.BussinessException;
import cn.lyd.base.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
     * 后台账号密码登录
     * @param loginVo
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody @Valid LoginVo loginVo){
        try {
          Map<String,Object> map = loginService.account(loginVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BussinessException e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后再试！");
        }
    }


    /**
     * 微信扫码登录
     * @param dto 前端传递的code：
     *            用户微信扫码后点击确认授权，
     *            在回调页面里截取到地址栏的code参数的值，
     *            发起post请求传递到后端
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody @Valid WechatDto dto){
        try {
            return loginService.wechat(dto);
        } catch (BussinessException e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后再试！");
        }
    }

    /**
     * 绑定
     * @param
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody @Valid BinderDto dto)
    {
        return loginService.binder(dto);
    }






}
